added SSCLI 1.0
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2010 / CSDynamicallyBuildLambdaExpressionWithField / DynamicCondition / SimulateVal.cs
blobab1ad86814f7844559eb8748159885353c18a095
1 /********************************** Module Header **********************************\
2 * Module Name: SimulateVal.cs
3 * Project: CSDynamicallyBuildLambdaExpressionWithField
4 * Copyright (c) Microsoft Corporation.
5 *
6 * The SimulateVal.cs file defines variant function which is deal with DateTime and Boolean.
8 * This source is subject to the Microsoft Public License.
9 * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
10 * All other rights reserved.
12 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
13 * EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
15 \***********************************************************************************/
17 public static class SimulateVal
19 public static double Val(string expression)
21 if (expression == null)
23 return 0;
25 // Try the entire string, then progressively smaller
26 // substrings to simulate the behavior of VB's 'Val',
27 // which ignores trailing characters after a recognizable value:
28 for (int size = expression.Length; size > 0; size--)
30 double testDouble;
31 if (double.TryParse(expression.Substring(0, size), out testDouble))
32 return testDouble;
35 // No value is recognized, so return 0:
36 return 0;
39 public static double Val(object expression)
41 if (expression == null)
43 return 0;
46 double testDouble;
47 if (double.TryParse(expression.ToString(), out testDouble))
49 return testDouble;
51 // CSharp's 'Val' function returns -1 for 'true':
52 bool testBool;
54 if (bool.TryParse(expression.ToString(), out testBool))
56 return testBool ? -1 : 0;
58 // CSharp's 'Val' function returns the day of the month for dates:
59 System.DateTime testDate;
60 if (System.DateTime.TryParse(expression.ToString(), out testDate))
62 return testDate.Day;
64 // No value is recognized, so return 0:
65 return 0;
69 /// <summary>
70 /// Convert char into string
71 /// </summary>
72 public static int Val(char expression)
74 int testInt;
75 if (int.TryParse(expression.ToString(), out testInt))
77 return testInt;
79 else
81 return 0;